﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using Aij1.Model;
using DevExpress.Data.Filtering;

namespace Aij1.BLL
{
    public class KeywordData
    {
        public static void Delete(Session session, string ids)
        {
            string sql = String.Format("DELETE FROM A_Keyword WHERE Oid IN ({0})", ids);
            session.ExecuteNonQuery(sql);
        }

        public static IList<A_Keyword> GetAllInUseKeywords(Session session)
        {
            XPCollection<A_Keyword> words = new XPCollection<A_Keyword>(session, CriteriaOperator.Parse("InUse=1"));
            words.Sorting.Add(new SortProperty("Title", DevExpress.Xpo.DB.SortingDirection.Ascending));
            return words;
        }

        /// <summary>
        /// 获取主要的关键词
        /// </summary>
        /// <param name="session"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static A_Keyword GetMainKeyword(Session session, string keyword)
        {
            A_Keyword key = session.FindObject<A_Keyword>(CriteriaOperator.Parse("Title LIKE '%'+?+'%'", keyword))
            ?? new A_Keyword(session)
            {
                Title = keyword,
                InUse = false,
            };
            key.UseTimes++;
            key.Save();

            while (key.ParentId != null) key = key.ParentId;
            return key;
        }
    }
}
